import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Leet970 {
    public static void main(String[] args) {
        Leet970 l = new Leet970();
        System.out.println(l.powerfulIntegers(2,3,10));
    }
    public List<Integer> powerfulIntegers(int x, int y, int bound) {
        int i = 0;
        int j;
        Set<Integer> list = new HashSet<>();
        while (true){
            double xx = Math.pow(x,i);
            if (xx>bound)
                break;
            j = 0;
            while (true){
                double yy = Math.pow(y,j);
                if (xx+yy>bound)
                    break;
                else list.add((int)(xx+yy));
                j++;
                if (y==1)
                    break;
            }
            i++;
            if (x==1)
                break;
        }
        return new ArrayList<>(list);
    }
}
